//app.js
import Config from 'config'
import Tools from 'utils/Tools'
import WxValidate from 'utils/WxValidate'
App({
  util: require('we7/resource/js/util.js'),
  tabBar: {
    "color": "#123",
    "selectedColor": "#1ba9ba",
    "borderStyle": "#1ba9ba",
    "backgroundColor": "#fff",
    "list": [
      {
        "pagePath": "/we7/pages/index/index",
        "iconPath": "/we7/resource/icon/home.png",
        "selectedIconPath": "/we7/resource/icon/homeselect.png",
        "text": "首页"
      },
      {
        "pagePath": "/we7/pages/user/index/index",
        "iconPath": "/we7/resource/icon/user.png",
        "selectedIconPath": "/we7/resource/icon/userselect.png",
        "text": "微擎我的"
      }
    ]
  },
  onLaunch: function () {
    var that = this;
    that.globalData.APPID = that.Config.APPID;
    that.globalData.APIURL = that.Config.APIURL;
    that.globalData.APPSTART = that.Config.APPSTART;
  },
  appstart: function () {
    var that = this;
    if (!that.globalData.shopName) {
      that.geShopName();
    }
    if (!that.globalData.openId) {
      that.getopenid();
    }
  },
  geShopName: function () {
   /*  var that = this;
    var service = 'Farm.getFarmInfo';
    wx.request({
      method: 'POST',
      url: that.globalData.APIURL,
      data: { service: service, appId: that.Config.APPID },
      header: { 'Content-Type': 'application/x-www-form-urlencoded' },
      success: function (res) {
        if (!res.data.data.code) {
          that.globalData.shopName = res.data.data.info.farmName
        }
      },
    }) */
  },
  getAppName: function () {
    var that = this;
    wx.setNavigationBarTitle({ title: that.Config.APPNAME });
  },
  getopenid: function () {
    var that = this;
    /* 获取登录信息开始 */
    //调用登录接口
    wx.login({
      success: function (loginCode) {
        var appidinfo = that.globalData.APPID;
        var secretinfo = that.globalData.APPSTART;
        /*
        console.log("service=" + service);
        console.log("appid=" + that.Config.APPID);
        console.log("jsCode=" + loginCode.code);
        */

        //调用request请求api转换登录凭证  
        wx.request({
          url: 'https://api.weixin.qq.com/sns/jscode2session?appid=' + appidinfo + '&secret=' + secretinfo + '&grant_type=authorization_code&js_code=' + loginCode.code,
          header: {
            'content-type': 'application/json'
          },
          success: function (res) {
            that.globalData.openId = res.data.openid;//设置openid
            that.getMyUserInfo();
            //that.getInviteCode();
          }
        })

      }
    })
    /* 获取登录信息结束 */
  },
  getMyUserInfo: function () {
    var that = this;
    wx.getUserInfo({
      success: function (res) {
        console.log('-------------------')
        console.log(res);
        that.globalData.userInfo = res.userInfo;
        //console.log(that.globalData.userInfo);
        //that.autoregInfo();
      }
    })
  },
  WxValidate: (rules, messages) => new WxValidate(rules, messages),
  Tools: new Tools,
  Config: Config,
  globalData: {
    APPID: '',
    APIURL: '',
    openId: '',
    shopName: '',
    myInviteCode: '',
    fromInviteCode: '',
    userInfo: null
  },
  globalData: {
    userInfo: null,
    i_tel: '../../abc_shop/resource/img/i_tel.png',
    i_add: '../../abc_shop/resource/img/i_add.png',
    i_chat: '../../abc_shop/resource/img/i_chat.png',
    i_home: '../../abc_shop/resource/img/i_home.png',
    i_time: '../../abc_shop/resource/img/i_time.png',
  },
  siteInfo: {
    'uniacid': '2',
    'acid': '2',
    'multiid': '0',
    'version': '1.0',
    'siteroot': 'http://xcx.itsfanda.com/app/index.php',
    'design_method': '3',
    'redirect_module': '',
    'template': ''
  }
});